package com.ghck.server.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ghck.server.dao.DoctorMapper;
import com.ghck.server.model.DepartmentDict;
import com.ghck.server.model.Doctor;
import com.ghck.server.model.HospitalDict;
import com.ghck.server.model.Users;
import com.ghck.server.model.query.DoctorQuery;
import com.ghck.server.model.response.DoctorRes;
import com.ghck.server.service.DepartmentDictService;
import com.ghck.server.service.DoctorService;
import com.ghck.server.service.HospitalDictService;
import com.ghck.server.service.UsersService;

/**
 * 医生  <br>
 * t_doctor <br>
 * v1.0 2016-05-07
 */
 @Service
 @Transactional
 public class DoctorServiceImpl implements DoctorService {
	
	private static final Logger logger = LoggerFactory.getLogger(DoctorServiceImpl.class);
	
	@Autowired
	private DoctorMapper doctorMapper;
	
	@Autowired 
	UsersService usersService;
	
	@Autowired
	HospitalDictService hospitalDictService;
	
	@Autowired
	DepartmentDictService departmentDictService;
	
	/**
 	*根据ID删除 医生
 	*/
 	@Override
 	public Integer deleteDoctorById(Integer id){
 		return doctorMapper.deleteById(id);
 	}
 	
 	/**
 	*新增 医生
 	*/
 	@Override
 	@Transactional
 	public Integer batchInsertDoctor(List<? extends Doctor> doctors){
		Integer count = doctorMapper.batchInsert(doctors);
		return count;
 	}
 	/**
 	*新增 医生
 	*/
 	@Override
 	@Transactional
 	public Integer insertDoctor(Doctor domain){
		Integer id = doctorMapper.insert(domain);
		return id;
 	}

 	/**
 	*更新 医生
 	*/
 	@Override
 	@Transactional
 	public Integer updateDoctor(Doctor domain){
		Integer count = doctorMapper.update(domain);
		return count;
 	}
 	
 	/**
 	*根据ID查询 医生
 	*/
 	public DoctorRes findDoctorById(Integer id){
 		DoctorRes doctor = doctorMapper.findByUserId(id);
 		if(doctor!=null){
 			doctor.dealWith();
 			DepartmentDict departmentDict = departmentDictService.findDepartmentDictByIdWithCache(doctor.getDepartmentId());
 			if(departmentDict!=null){
 				doctor.setDepartmentName(departmentDict.getName());
 			}
 			HospitalDict hospitalDict = hospitalDictService.findByIdInCache(doctor.getHospitalId());
 			if(hospitalDict!=null){
 				doctor.setHospitalName(hospitalDict.getName());
 			}
 			Users user = usersService.findUsersById(id);
 			doctor.copyField(user);
 		}
 		return doctor;
 	}

	/**
 	*通过query 查询 医生
 	*/
	public List<DoctorRes> findDoctorByQuery(DoctorQuery query){
		query.dealWith();
		List<DoctorRes> result = doctorMapper.findByQuery(query);
		List<Integer> userIds = new ArrayList<Integer>();
		List<Integer> hospitalIds = new ArrayList<Integer>();
		if(CollectionUtils.isNotEmpty(result)) {
			Map<Integer, DoctorRes> map = new HashMap<Integer, DoctorRes>();
			for (DoctorRes doctor : result) {
				doctor.dealWith();
				userIds.add(doctor.getUserId());
				hospitalIds.add(doctor.getHospitalId());
				map.put(doctor.getUserId(), doctor);
				DepartmentDict departmentDict = departmentDictService.findDepartmentDictByIdWithCache(doctor.getDepartmentId());
				if(departmentDict!=null){
					doctor.setDepartmentName(departmentDict.getName());
				}
				HospitalDict hospitalDict = hospitalDictService.findByIdInCache(doctor.getHospitalId());
				if(hospitalDict!=null){
					doctor.setHospitalName(hospitalDict.getName());
				}
			}
			List<Users> users = usersService.findUsersByIds(userIds);
			if (CollectionUtils.isNotEmpty(users)) {
				for (Users user : users) {
					map.get(user.getId()).copyField(user);
				}
			}
			
		}
		
		return result;
	}	
	
	/**
 	*通过query count总条数
 	*/
 	public Integer countDoctorByQuery(DoctorQuery query){
 		query.dealWith();
 		Integer result = doctorMapper.countByQuery(query);
 		return result;
 	}
 	
 	/**
 	* 通过ids查询
 	*/
 	public List<DoctorRes> findDoctorByUserIds(List<Integer> ids){
 		List<DoctorRes> result = doctorMapper.findByUserIds(ids);
 		List<Integer> userIds = new ArrayList<Integer>();
		List<Integer> hospitalIds = new ArrayList<Integer>();
		if(CollectionUtils.isNotEmpty(result)) {
			Map<Integer, DoctorRes> map = new HashMap<Integer, DoctorRes>();
			for (DoctorRes doctor : result) {
				doctor.dealWith();
				userIds.add(doctor.getUserId());
				hospitalIds.add(doctor.getHospitalId());
				map.put(doctor.getUserId(), doctor);
				DepartmentDict departmentDict = departmentDictService.findDepartmentDictByIdWithCache(doctor.getDepartmentId());
				if(departmentDict!=null){
					doctor.setDepartmentName(departmentDict.getName());
				}
				HospitalDict hospitalDict = hospitalDictService.findByIdInCache(doctor.getHospitalId());
				if(hospitalDict!=null){
					doctor.setHospitalName(hospitalDict.getName());
				}
			}
			List<Users> users = usersService.findUsersByIds(userIds);
			if (CollectionUtils.isNotEmpty(users)) {
				for (Users user : users) {
					map.get(user.getId()).copyField(user);
				}
			}
			
		}
		
		return result;
 	}
 	
}	